package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.GoodsCommentService;
import com.baichang.taoshunda.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/8/17 0017.
 */
@Controller
@RequestMapping(ControllerConstants.ORDER)
public class OrderWController extends WebBaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsCommentService goodsCommentService;

    /**
     * 跳转订单页面
     *
     * @return
     */
    @RequestMapping("/findOrderIndex")
    public String findOrder() {
        return "taoshunda/order/allOrder";
    }

    /**
     * 跳转预约订单页面
     *
     * @return
     */
    @RequestMapping("/findAppointmentOrder")
    public String findAppointmentOrder() {
        return "taoshunda/order/appointmentOrder";
    }


    /**
     * 查询订单列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryOrder")
    public void queryOrder(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String name = getString(request, "name", "");
                String nameUser = getString(request, "nameUser", "");
                String orderNumber = getString(request, "orderId", "");
                Integer orderState = getInt(request, "state", -1);

                return orderService.queryOrder(name, nameUser, orderNumber, orderState, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询订单详细
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryOrderDetail")
    public void queryOrderDetail(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String orderNumber = getString(request, "orderNumber", "");
                return orderService.queryOrderDetail(orderNumber, nowPage, pageSize);
            }
        });
    }

//    /**
//     * 同意退款、拒绝退款
//     *
//     * @param request
//     * @param response
//     */
//    @RequestMapping("/updateOrderState")
//    public void updateOrderState(final HttpServletRequest request, HttpServletResponse response) {
//        init(request, response, new IController() {
//            @Override
//            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
//                Integer orderId = getInt(request, "id", 0);
//                Integer state = getInt(request, "state", 0);
//                Integer buyerId = getInt(request, "buyerId", 0);
//                Integer goodsId = getInt(request, "goodsId", 0);
//                Integer payType = getInt(request, "payType", 0);
//                Integer stock = getInt(request, "stock", 0);
//                String reason = getString(request, "reason", "");
//                String orderNumber = getString(request, "orderNumber", "");
//                return orderService.updateOrderState(orderId, state, goodsId, payType, stock, reason, orderNumber);
//            }
//        });
//    }

//    /**
//     * 商家拒单
//     *
//     * @param request
//     * @param response
//     */
//    @RequestMapping("/refuseOrder")
//    public void refuseOrder(final HttpServletRequest request, HttpServletResponse response) {
//        init(request, response, new IController() {
//            @Override
//            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
//                Integer id = getInt(request, "id", 0);
//                Integer state = getInt(request, "state", 0);
//                Integer buyerId = getInt(request, "buyerId", 0);
//                String orderNumber = getString(request, "orderNumber", "");
//                String reason = getString(request, "reason", "");
//                Integer payType = getInt(request, "payType", 0);
//                return orderService.refuseOrder(id, state, buyerId, orderNumber, reason, payType);
//            }
//        });
//    }

    /**
     * 查询订单评论
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryOrderComment")
    public void queryOrderComment(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer orderId = getInt(request, "orderId", 0);
                return goodsCommentService.queryOrderComment(orderId, nowPage, pageSize);
            }
        });
    }

    /**
     * 预约订单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryAppointmentOrder")
    public void queryAppointmentOrder(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String userName = getString(request, "userName", "");
                String busName = getString(request, "busName", "");
                String orderNumber = getString(request, "orderId", "");
                Integer orderState = getInt(request, "state", -1);
                return orderService.queryAppointmentOrder(userName, busName, nowPage, pageSize, orderNumber, orderState);
            }
        });
    }

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/queryReservationOrder")
    public void queryReservationOrder(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String orderNumber = getString(request, "orderNumber", "");
                return orderService.queryReservationOrder(orderNumber, nowPage, pageSize);
            }
        });
    }
}
